package com.app.bimo.commonbusiness.base;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.app.bimo.commonbusiness.R;
import com.app.bimo.commonbusiness.mvp.IPresenter;
import com.app.bimo.commonbusiness.mvp.IView;
import com.app.bimo.commonbusiness.util.DataUtil;
import com.app.bimo.commonbusiness.util.ToastUtils;
import com.app.bimo.commonbusiness.view.DialogUtil;
import com.app.bimo.commonbusiness.view.NetworkStateView;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.app.bimo.commonbusiness.base.BaseApplication.getContext;

public abstract class BaseActivity<P extends IPresenter> extends AppCompatActivity implements IView,NetworkStateView.OnRefreshListener {

    private NetworkStateView networkStateView;
    protected Toolbar mtoolbar;

    @Nullable
    protected P mPresenter;

    private Unbinder unbinder;
    private Dialog loadingDialg;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }


        //基础布局
        View view = getLayoutInflater().inflate(R.layout.layout_base_activity, null);
        //设置填充activity_base布局
        setContentView(view);
        //加载子类Activity的布局
        initDefaultView(setLayoutID());
        //绑定子控件
        unbinder = ButterKnife.bind(this);
//        loadingDialg = DialogUtil.initLoadingDialog(getContext());

        initView();
        initData();
        initEvent();


    }

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initEvent();

    protected abstract int setLayoutID();


    /**
     *初始化默认view  并添加各子类页面布局id
     * @param layoutResID  布局id
     */
    private void initDefaultView(int layoutResID) {
        networkStateView = findViewById(R.id.nsv_state_view);
        ConstraintLayout container = findViewById(R.id.fl_activity_child_container);
        LayoutInflater.from(this).inflate(layoutResID, container,true);
    }


//    /**
//     * 显示加载完成后的布局(即子类Activity的布局)
//     */
//    public void showContentView() {
//        networkStateView.showSuccess();
//    }


    /**
     * 刷新
     */
    @Override
    public void onBaseRefresh() {
        networkStateView.showLoading();
        showSuccess();
    }


    /**
     * 表示成功渲染
     */
    private void showSuccess() {
        BaseApplication.getMainThreadHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                networkStateView.showSuccess();
            }
        }, 2000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解绑
        unbinder.unbind();
    }


    @Nullable
    public final <T extends View> T getView(@IdRes int id) {
        if (id == View.NO_ID) {
            return null;
        }
        return findViewById(id);
    }






    //***************************************************************************//

    @Override
    public void showLogin() {

    }

    @Override
    public void showLoading() {
        networkStateView.showLoading();
    }

    @Override
    public void hideLoading() {
        networkStateView.showSuccess();
    }

    @Override
    public void showMessage(@NonNull String message) {
        if (!DataUtil.isEmpty(message)) {
            ToastUtils.showToast(getContext(), message);
        }
    }

    /**
     * 显示没有网络的布局
     */
    @Override
    public void showError() {
        networkStateView.showNoNetwork();
        networkStateView.setOnRefreshListener(this);
    }

    /**
     * 显示没有数据的布局
     */
    @Override
    public void showEmpty() {
        networkStateView.showEmpty();
        networkStateView.setOnRefreshListener(this);
    }

    @Override
    public void showDialogLoading() {
//        loadingDialg.show();
    }

    @Override
    public void hideDialogLoading() {
//        loadingDialg.dismiss();
    }

    @Override
    public Context getMContext() {
        return this;
    }
}
